<?php

require_once('module_resourcepage.php');

/**
 * Base class for resourcepage extensions. Contains shared functionality
 * such as that covering database updates, etc.
 *  
 * @copyright &copy; 2006 The Open University
 * @author s.marshall@open.ac.uk
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
 * @package resourcepage 
 */
abstract class resourcepage_extension {
    
    private $name;
    
    // Resourcepage reference so you can can call utility things from there
    protected $resourcepage;

    /**
     * Constructs extension object.
     * @param object $resourcepage Resourcepage module object
     */ 
    function __construct(&$resourcepage) {
        // Check classname and extract module name from it
        $classname=get_class($this);
        $matches=array();
        if(!preg_match('/^resourcepage_([a-z]+)$/',$classname,$matches)) {
            throw new Exception(
                'Extension class name not valid; must be resourcepage_lowercaseletters: '.$classname); 
        }        
        $this->name=$matches[1];
        
        $this->resourcepage =& $resourcepage;
    }
    
    /**
     * @return string Name (ID) of extension.
     */
    public function get_name() {
        return $this->name;
    }
    
    /**
     * @return string Name of main table for this extension.
     */
    protected function get_maintable_name() {
        global $CFG;
        return $CFG->prefix.'resourcepage_x_'.$this->name;
    }
    
    /**
     * Obtains filename of database upgrade file. (May be overridden.)
     * @return string Filename of the database upgrade file for this extension.
     */
    public function get_upgrade_filename() {
        global $CFG;
        return $CFG->dirroot.'/mod/resourcepage/extensions/'.$this->name.'/db/upgrade.php';
    }

    /**
     * Obtains filename of database install file. (May be overridden.)
     * @return string Filename of the database install file for this extension.
     */
    protected function get_install_filename() {
        global $CFG;
        return $CFG->dirroot.'/mod/resourcepage/extensions/'.$this->name.'/db/install.xml';
    }

    /**
     * Obtains filename of 'edit' form description. (May be overridden.)
     * @return string Filename of the 'edit' file for this extension.
     */
    protected function get_edit_form_filename() {
        global $CFG;
        return $CFG->dirroot.'/mod/resourcepage/extensions/'.$this->name.'/edit_form.php';
    }
    
    /**
     * @return bool True if there's an edit.xml, false otherwise.
     */
    public function has_edit_page() {
        return file_exists($this->get_edit_form_filename());
    }
    
    
    /** 
     * Installs database tables. Default version reads and processes tables.xml
     * if present.
     */
    public function install() {        

        // Both old .sql files and new install.xml are supported
        // but we priorize install.xml (XMLDB) if present
        $installfilename = $this->get_install_filename();
        if (file_exists($installfilename)) {
            install_from_xmldb_file($installfilename); //New method
        }
    }
    
    /**
     * Uninstalls database tables. Default version reads and processes tables.xml
     * at present.
     */
    public function uninstall() {
    }
    
    /**
     * Called to get the database version of the extension. Must be 
     * overridden to return a string in the format YYYYMMDDnn e.g.
     * 2005112901. This version should change only when the database
     * content format changes i.e. it needs upgrading.
     * @return string Module database version string in standard format
     *   YYYYMMDDnn e.g. 2005112901 
     */
    public abstract function get_database_version();
    
    /**
     * Called to check which Moodle version the extension requires. Override
     * to change.
     * @return string Moodle version string in standard format (default
     *   2004052505)
     */
    public function get_required_moodle() {
        return '2004052505';
    }    
    /**
     * Called to check which resourcepage version the extension requires. 
     * Override to change.
     * @return string Resourcepage version string in standard format (default
     *   2006010101)
     */
    public function get_required_resourcepage() {
        return '2006010101';
    }
        
    /**
     * Returns how often the extension wants to be hit by cron job. 
     * Default doesn't require cron access. Override to change. Note that
     * although it's specified in seconds, if the overall cron job runs
     * less frequently, you won't get it. Realistically, cron jobs may
     * occur e.g. every 5 minutes.
     * @return int Desired period between cron accesses, in seconds; 0
     *   if cron calls are not required.
     */
    public function get_cron_frequency() {
        return 0;
    }    
    
    /**
     * Called when the module gets cronned. Override to actually do 
     * something. Default does nothing.
     */
    public function cron() {
    }
    
    /**
     * Runs an SQL statement, automatically replacing extprefix_ with the 
     * extension's prefix, modprefix_ with the module's prefix,
     * and prefix_ with the base Moodle prefix. Throws an exception if there is any SQL error.
     * @param string $sql SQL statement
     * @return object ADODB result set
     */
    public function do_sql($sql) {
        return $this->resourcepage->do_sql(preg_replace('/extprefix_/',
            $this->get_maintable_name().'_',$sql));
    }     
    
    protected function c($string) {
        return sql_char($string);
    }
    
    /**
     * Called just before an entry from resourcepage_containers is deleted. 
     * Should delete any entries from extension table that reference the 
     * container.
     * @param int $containerid ID of container
     * @throws Exception if any problems 
     */
    public function before_deleting_container($containeris) {
    }
    
    /**
     * Called just before an entry from resourcepage main table is deleted. 
     * Should delete any entries from extension table that reference the 
     * resourcepage.
     * @param object $record Entry from resourcepage table
     * @throws Exception if any problems 
     */
    public function before_deleting_resourcepage($record) {
    }
    
    /**
     * Called after deleting all resourcepage instances, before deleting
     * the course entry.
     * @param int $courseid Course ID 
     */
    public function before_deleting_course($courseid) {
    }
}

?>
